package com.hbyj.struts.action;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.CDBInfo;
import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Functions;
import com.hbyj.hbm.vo.FzyaFzya03;
import com.hbyj.hbm.vo.FzyaFzya03temp;
import com.hbyj.hbm.vo.Jbqk01;
import com.hbyj.hbm.vo.Jbqk01temp;
import com.hbyj.hbm.vo.JcdcJcdc00;
import com.hbyj.hbm.vo.JcdcJcdc01;
import com.hbyj.hbm.vo.JcdcJcdc02;
import com.hbyj.hbm.vo.JcdcJcdc03;
import com.hbyj.hbm.vo.JcdcJcdc06;
import com.hbyj.hbm.vo.JcdcJcdc07;
import com.hbyj.hbm.vo.YjczYjzy01;
import com.hbyj.hbm.vo.YjczYjzy02;
import com.hbyj.hbm.vo.YjczYpxx01;
import com.hbyj.object.ZTreeNode;
import com.hbyj.util.ChangeTool;
import com.hbyj.util.Constants;
import com.hbyj.util.JsonUtil;
import com.hbyj.util.PageClass;
import com.opensymphony.xwork2.ActionSupport;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


public class BaseInfoQueryAction extends ActionSupport{
	private int total;
	private List rows = new ArrayList();
	private List groupList = new ArrayList();
	private List rowsFor5w = new ArrayList();
	private String jsonResult;
	private Jbqk01 jbqk = new Jbqk01();
	public String getJsonResult() {
		return jsonResult;
	}
	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public List getRows() {
		return rows;
	}
	public void setRows(List rows) {
		this.rows = rows;
	}
	
	public List getGroupList() {
		return groupList;
	}
	public void setGroupList(List groupList) {
		this.groupList = groupList;
	}
	
	public List getRowsFor5w() {
		return rowsFor5w;
	}
	public void setRowsFor5w(List rowsFor5w) {
		this.rowsFor5w = rowsFor5w;
	}
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 基本查询
	 * @return
	 */
	public String findJbqk01ListByCondition(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		int pageSize = Constants.PAGE_KEY;
		String areaCode = request.getParameter("region");
		String zhdType = request.getParameter("zhdType");
		String name = request.getParameter("name");
		areaCode = ChangeTool.ChangeArea(areaCode);
		String tableName = "";
		String propertyName = "";
		String hql = "";
		String countHql = "";
		String conStr = "";
		String conditionHql = "";
		String groupCountHql = "";//分组统计Hql
		if(!zhdType.equals("all"))
			conStr = " and jbqk01.id.jbqk0102='"+zhdType+"'";
		/*else{
			switch(Integer.parseInt(zhdType)){
			case 1:
				tableName = "";
				break;
			case 2:break;
			case 3:break;
			case 4:break;
			case 5:break;
			}
		}*/
		//hql = "select distinct jbqk01.id.jbqk0100,jbqk01.jbqk0110,jbqk01.jbqk0120, jbqk01.jbqk0130,jbqk01.jbqk0140,jbqk01.jbqk0150,jbqk01.jbqk0160,jbqk01.jbqk0170 from Jbqk01 as jbqk01,"+tableName+" as subTbl where jbqk01.id.jbqk0100=subTbl."+propertyName+" and jbqk01.id.jbqk0100 like '"+areaCode+"%'"+conStr;
		//countHql = "select count(distinct jbqk01.id.jbqk0100)   from Jbqk01 as jbqk01,"+tableName+" as subTbl  where jbqk01.id.jbqk0100=subTbl."+propertyName+" and jbqk01.id.jbqk0100 like '"+areaCode+"%'"+conStr;
		conditionHql = "jbqk01.id.jbqk0100 like '"+areaCode+"%' and jbqk01.jbqk0110 like '%"+name+"%'"+conStr;
		hql = "select distinct jbqk01.id.jbqk0100,jbqk01.jbqk0110,jbqk01.jbqk0120, jbqk01.jbqk0130,jbqk01.jbqk0140,jbqk01.jbqk0150,jbqk01.jbqk0160,jbqk01.jbqk0170,jbqk01.jbqk0180,jbqk01.jbqk0190,jbqk01.id.jbqk0102 from Jbqk01 as jbqk01 where   "+conditionHql;
		countHql = "select count(distinct jbqk01.id.jbqk0100)   from Jbqk01 as jbqk01  where  "+conditionHql;
		groupCountHql = "select jbqk01.id.jbqk0102,count(jbqk0102) from Jbqk01 as jbqk01  where   "+conditionHql+" group by jbqk01.id.jbqk0102";
		 groupList = DbTools.findByHql(groupCountHql);
		int t = DbTools.getDataHQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		//List<List> list = (List)DbTools.findPageHQL(first, max,hql);
		rows = DbTools.parseBaleInfoObj(DbTools.findPageHQL(first, max,hql));
		total = t;
		return SUCCESS;
	}
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 查询放在预案基本信息
	 * @return
	 */
	public String findFzyaDetailsByCode(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String code = request.getParameter("code");
		String zhdName = request.getParameter("zhdName");
		String hql = " from Jbqk01 ,FzyaFzya03  where jbqk0100=fzya03000 and fzya03000='"+code+"'";
		rows = DbTools.findPageHQL(0,1,hql);
		request.setAttribute("fzyaDetails", rows);
		request.setAttribute("zhdName", zhdName);
		return SUCCESS;
	}
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 高级搜索
	 * @return
	 */
	public String findJbqk01ListByAdvanceCondition(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNo = request.getParameter("page");
		String region = request.getParameter("region"); 
		String zhdType = request.getParameter("zhdType");
		String zhdGroupType = request.getParameter("zhdGroupType");
		String graphParam = request.getParameter("graphParam");//图形范围参数  圆  x,y,r  矩形 xMin,yMin,xMax,yMax
		int pageSize = Constants.PAGE_KEY;
		if(pageNo==null){ 
			pageSize =500;
			pageNo = "1";
		} 
		String countHql = "";
		String conStr = "";
		String conditionHql = "";
		String groupCountHql = "";//分组统计Hql
		String hql = "";
		region = ChangeTool.ChangeArea(region);
		if(!"".equals(zhdType)){
			conStr +=" and substring(jbqk01.id.jbqk0100,7,2) in ("+formatCondition(zhdType)+")";
		}
		if(!"".equals(graphParam)){
			String[] graphParamArr = graphParam.split(",");
			if(graphParamArr.length==3){
				conStr +=" and ((jbqk0120-"+graphParamArr[0]+")*(jbqk0120-"+graphParamArr[0]+")+(jbqk0130-"+graphParamArr[1]+")*(jbqk0130-"+graphParamArr[1]+"))< "+graphParamArr[2]+"*"+graphParamArr[2];
			}else{
				conStr +=" and jbqk0120 >"+graphParamArr[0]+" and jbqk0120<"+graphParamArr[2]+" and  jbqk0130>"+graphParamArr[1]+" and  jbqk0130<"+graphParamArr[3];
			}
			
		}
		if(!"".equals(zhdGroupType)){
			conStr += " and jbqk01.id.jbqk0102 in ("+formatCondition(zhdGroupType)+")";
		}
		conditionHql = "jbqk01.id.jbqk0100 like '"+region+"%' " + conStr;
		hql = "select distinct jbqk01.id.jbqk0100,jbqk01.jbqk0110,jbqk01.jbqk0120, jbqk01.jbqk0130,jbqk01.jbqk0140,jbqk01.jbqk0150,jbqk01.jbqk0160,jbqk01.jbqk0170,jbqk01.jbqk0180,jbqk01.jbqk0190,jbqk01.id.jbqk0102 from Jbqk01 as jbqk01 where   "+conditionHql;
		countHql = "select count(distinct jbqk01.id.jbqk0100)   from Jbqk01 as jbqk01  where  "+conditionHql;
		groupCountHql = "select jbqk01.id.jbqk0102,count(jbqk0102) from Jbqk01 as jbqk01  where   "+conditionHql+" group by jbqk01.id.jbqk0102";
		 groupList = DbTools.findByHql(groupCountHql);
		int t = DbTools.getDataHQLCount(countHql);
		PageClass page = new PageClass();
		page.setPageShow(pageSize);  //设置每页显示的行数
		page.setPageCount(t);  //设置总页数
		page.setCurrentPage(Integer.parseInt(pageNo));  //设置当前页
		int first=page.getNextIndex(); //获得开始的索引值
		int max=page.getPageShow();  //获得每次显示的值
		//List<List> list = (List)DbTools.findPageHQL(first, max,hql);
		rows = DbTools.parseBaleInfoObj(DbTools.findPageHQL(first, max,hql));
		total = t;
		System.out.println(t);
		return SUCCESS;
	}
	
	/**
	 * author cuiyi
	 * time 2013-1-8
	 * disc 点击图片后，查看基本详情信息
	 * @return
	 */
	public String lookIntroduce(){
		HttpServletRequest request = ServletActionContext.getRequest();
		//地质灾害统一编码
		String name = request.getParameter("name");//灾点名称
		String conURL = request.getParameter("conURL");//详情名称  word
		String file = request.getParameter("fileName");
		String webRootPath = ServletActionContext.getServletContext().getRealPath("/")+"uploadfolder\\simple\\"+file+"\\document\\";
		if(!conURL.equals("null")){
			conURL = webRootPath + conURL ;
			//String[] con= ChangeTool.getWordContent(conURL);
			String con = ChangeTool.readTxtFile(conURL);
			request.setAttribute("Contetnt", con);
		}
		String imgURL = request.getParameter("imgURL");//图片路径
		request.setAttribute("file", file);
		request.setAttribute("name", name);
		request.setAttribute("imgURL", imgURL);
		return SUCCESS;
	}
	public String findJCDCBaseInfoByCode(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String code = request.getParameter("code");
		String groupNum = request.getParameter("groupNum");
		String flag = code.substring(6, 8);
		String zhdName = request.getParameter("zhdName");
		String hql = "";
		switch (Integer.parseInt(groupNum)) {
		case 1:///10万基础调查
			hql = " from Jbqk01 ,JcdcJcdc"+flag+"  where jbqk0100=jcdc"+flag+"001 and jcdc"+flag+"001='"+code+"' and jbqk0102='"+groupNum+"'";
			rows = DbTools.findPageHQL(0,10,hql);
			break;
		case 2:///5万详细调查
			hql = " from Jbqk01 ,XxdcXxdc"+flag+"  where jbqk0100=xxdc"+flag+"0040 and xxdc"+flag+"0040='"+code+"' and jbqk0102='"+groupNum+"'";
			rowsFor5w = DbTools.findPageHQL(0,10,hql);
			break;
		case 3://应急调查
			hql = " from Jbqk01 jbqk,YjdcYjdc01 yjdc  where jbqk0100=yjdc01000 and jbqk.id.jbqk0102=yjdc.jbqk0102  and yjdc01000='"+code+"' ";
			rows = DbTools.findPageHQL(0,10,hql);
			break;
		case 4://群测群防
			hql = " from Jbqk01 ,QcqfQcqf01  where jbqk0100=qcqf0100 and qcqf0100='"+code+"' and jbqk0102='"+groupNum+"'";
			rows = DbTools.findPageHQL(0,10,hql);
			break;
		case 5://重大灾害
			hql = " from Jbqk01 ,ZdzqZdzq01  where jbqk0100=zdzq01000 and zdzq01000='"+code+"' and jbqk0102='"+groupNum+"'";
			rows = DbTools.findPageHQL(0,10,hql);
			break;
		case 6://专业监测
			hql = " from Jbqk01 ,ZyqcZyjc01  where jbqk0100=zyjc0100 and zyjc0100='"+code+"' and jbqk0102='"+groupNum+"'";
			rows = DbTools.findPageHQL(0,10,hql);
			break;
		}
		
		
		request.setAttribute("zhdName", zhdName);
		return SUCCESS;
	}
	public String downFzyaTableByCode(){
		//设置模板装置的方法和路径
		//我们的模板是防灾src.wordmodel下
		//开始读取模板
		jsonResult = "false";
		HttpServletRequest request = ServletActionContext.getRequest();
		String root = ServletActionContext.getServletContext().getRealPath("/");; //得到网站绝对路径
		String code = request.getParameter("code");
		String zhdName = request.getParameter("zhdName");
		String qType = request.getParameter("qType");//查询类型  0 已有点  1新增点
		String tbl = "FzyaFzya03";
		String tblJBQK01 = "Jbqk01"; 
		if(qType!=null&&qType.equals("1")){
			tbl = "FzyaFzya03temp";
			tblJBQK01 = "Jbqk01temp";
		}
		String hql = " from  "+tbl+","+tblJBQK01+"   where jbqk0100=fzya03000 and fzya03000='"+code+"'";
		List list = DbTools.findPageHQL(0,1,hql);
		if(list.size()==0){
			return SUCCESS;
		}
		Configuration configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
		configuration.setClassForTemplateLoading(this.getClass(), "wordmodel");
		Template t = null;
		//填入数据
		Map dataMap = getFzyaData(list,qType);
		try {
			t = configuration.getTemplate("fzya.xml","utf-8");
			
			//开始输出word
			//java.util.Date date = new java.util.Date();
			//String dateName = String.valueOf(date.getTime());
			//File f = new File(root + filepath + fileName);
			File outFile = new File(root+"\\uploadfolder\\fzyaFiles\\"+code+".doc");
			if(outFile.exists()){
				outFile.delete();
			}
			outFile.createNewFile();
			Writer out = null;
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));
			t.process(dataMap, out);
			out.close();
			jsonResult = "true$"+code+".doc";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	private Map getFzyaData(List list,String qType){
		Map dataMap = new HashMap();
		Object[] obj = (Object[])list.get(0);
		
		if(qType!=null&&qType.equals("1")){
			Jbqk01temp jbqk = (Jbqk01temp)obj[1];
			FzyaFzya03temp fzya = (FzyaFzya03temp)obj[0];
			dataMap.put("zhdName", jbqk.getJbqk0110()==null?"":jbqk.getJbqk0110());
			dataMap.put("FZYA03000", fzya.getFzya03000()==null?"":fzya.getFzya03000());
			dataMap.put("FZYA03160", fzya.getFzya03160()==null?"":fzya.getFzya03160());
			dataMap.put("FZYA03170", fzya.getFzya03170()==null?"":fzya.getFzya03170());
			dataMap.put("FZYA03180", fzya.getFzya03180()==null?"":fzya.getFzya03180());
			dataMap.put("FZYA03190", fzya.getFzya03190()==null?"":fzya.getFzya03190());
			dataMap.put("FZYA03200", fzya.getFzya03200()==null?"":fzya.getFzya03200());
			dataMap.put("FZYA03210", fzya.getFzya03210()==null?"":fzya.getFzya03210());
			dataMap.put("FZYA03220", fzya.getFzya03220()==null?"":fzya.getFzya03220());
			dataMap.put("FZYA03230", fzya.getFzya03230()==null?"":fzya.getFzya03230());
			dataMap.put("FZYA03240", fzya.getFzya03240()==null?"":fzya.getFzya03240());
			dataMap.put("FZYA03250", fzya.getFzya03250()==null?"":fzya.getFzya03250());
			dataMap.put("FZYA03260", fzya.getFzya03250()==null?"":fzya.getFzya03250());
			dataMap.put("FZYA03270", fzya.getFzya03270()==null?"":fzya.getFzya03270());
			dataMap.put("FZYA03280", fzya.getFzya03280()==null?"":fzya.getFzya03280());
			dataMap.put("FZYA03290", fzya.getFzya03290()==null?"":fzya.getFzya03290());
			dataMap.put("FZYA03300", fzya.getFzya03300()==null?"":fzya.getFzya03300());
			dataMap.put("FZYA03310", fzya.getFzya03310()==null?"":fzya.getFzya03310());
			dataMap.put("FZYA03320", fzya.getFzya03320()==null?"":fzya.getFzya03320());
			dataMap.put("FZYA03330", fzya.getFzya03330()==null?"":fzya.getFzya03330());
			dataMap.put("FZYA03340", fzya.getFzya03340()==null?"":fzya.getFzya03340());
			dataMap.put("FZYA03350", fzya.getFzya03350()==null?"":fzya.getFzya03350());
			dataMap.put("FZYA03360", fzya.getFzya03360()==null?"":fzya.getFzya03360());
			dataMap.put("FZYA03370", fzya.getFzya03370()==null?"":fzya.getFzya03370());
			dataMap.put("FZYA03380", fzya.getFzya03380()==null?"":fzya.getFzya03380());
			dataMap.put("FZYA03390", fzya.getFzya03390()==null?"":fzya.getFzya03390());
			dataMap.put("FZYA03400", fzya.getFzya03400()==null?"":fzya.getFzya03400());
			dataMap.put("FZYA03410", fzya.getFzya03410()==null?"":fzya.getFzya03410());
			dataMap.put("FZYA03420", fzya.getFzya03420()==null?"":fzya.getFzya03420());
			dataMap.put("FZYA03430", fzya.getFzya03430()==null?"":fzya.getFzya03430());
			dataMap.put("FZYA03440", fzya.getFzya03440()==null?"":fzya.getFzya03440());
		}else {
			Jbqk01 jbqk = (Jbqk01)obj[1];
			FzyaFzya03 fzya = (FzyaFzya03)obj[0];
			dataMap.put("zhdName", jbqk.getJbqk0110());
			dataMap.put("FZYA03000", fzya.getFzya03000()==null?"":fzya.getFzya03000());
			dataMap.put("FZYA03160", fzya.getFzya03160()==null?"":fzya.getFzya03160());
			dataMap.put("FZYA03170", fzya.getFzya03170()==null?"":fzya.getFzya03170());
			dataMap.put("FZYA03180", fzya.getFzya03180()==null?"":fzya.getFzya03180());
			dataMap.put("FZYA03190", fzya.getFzya03190()==null?"":fzya.getFzya03190());
			dataMap.put("FZYA03200", fzya.getFzya03200()==null?"":fzya.getFzya03200());
			dataMap.put("FZYA03210", fzya.getFzya03210()==null?"":fzya.getFzya03210());
			dataMap.put("FZYA03220", fzya.getFzya03220()==null?"":fzya.getFzya03220());
			dataMap.put("FZYA03230", fzya.getFzya03230()==null?"":fzya.getFzya03230());
			dataMap.put("FZYA03240", fzya.getFzya03240()==null?"":fzya.getFzya03240());
			dataMap.put("FZYA03250", fzya.getFzya03250()==null?"":fzya.getFzya03250());
			dataMap.put("FZYA03260", fzya.getFzya03250()==null?"":fzya.getFzya03250());
			dataMap.put("FZYA03270", fzya.getFzya03270()==null?"":fzya.getFzya03270());
			dataMap.put("FZYA03280", fzya.getFzya03280()==null?"":fzya.getFzya03280());
			dataMap.put("FZYA03290", fzya.getFzya03290()==null?"":fzya.getFzya03290());
			dataMap.put("FZYA03300", fzya.getFzya03300()==null?"":fzya.getFzya03300());
			dataMap.put("FZYA03310", fzya.getFzya03310()==null?"":fzya.getFzya03310());
			dataMap.put("FZYA03320", fzya.getFzya03320()==null?"":fzya.getFzya03320());
			dataMap.put("FZYA03330", fzya.getFzya03330()==null?"":fzya.getFzya03330());
			dataMap.put("FZYA03340", fzya.getFzya03340()==null?"":fzya.getFzya03340());
			dataMap.put("FZYA03350", fzya.getFzya03350()==null?"":fzya.getFzya03350());
			dataMap.put("FZYA03360", fzya.getFzya03360()==null?"":fzya.getFzya03360());
			dataMap.put("FZYA03370", fzya.getFzya03370()==null?"":fzya.getFzya03370());
			dataMap.put("FZYA03380", fzya.getFzya03380()==null?"":fzya.getFzya03380());
			dataMap.put("FZYA03390", fzya.getFzya03390()==null?"":fzya.getFzya03390());
			dataMap.put("FZYA03400", fzya.getFzya03400()==null?"":fzya.getFzya03400());
			dataMap.put("FZYA03410", fzya.getFzya03410()==null?"":fzya.getFzya03410());
			dataMap.put("FZYA03420", fzya.getFzya03420()==null?"":fzya.getFzya03420());
			dataMap.put("FZYA03430", fzya.getFzya03430()==null?"":fzya.getFzya03430());
			dataMap.put("FZYA03440", fzya.getFzya03440()==null?"":fzya.getFzya03440());
		}
		return dataMap;
		
	}
	private String  formatCondition(String str){
		String[] s = str.split(",");
		String rs = "'";
		for(int i=0;i<s.length;i++){
			rs+=s[i]+"','";
		}
		return rs.substring(0, rs.length()-2);
	}
}
